<template>
  <div>
    <!-- 按钮权限 -->
    <!-- {{$refs.tree.getCheckedKeys()}} -->
    <el-tree :data="T_data" show-checkbox node-key="id" ref="tree" style="background: #f0f4fa;" :default-checked-keys="C_arr_data" :current-node-key="cnkarr" :props="defaultPropsLeft" @node-click="NodeClick" @check-change="checkChange"></el-tree>
  </div>
</template>
<script>
import { elementEcho } from '@/api/Tmanager'
export default {
  props: ['CheckedBtnArr', 'rowClone', 'pc', 'TreeSaveNode'],
  data() {
    return {
      CheckedBtnArrId: [], // 初始的id
      //选中的
      C_arr_data: [],
      checkChangeData: [],
      //   C3:this.$refs.tree.getCheckedKeys(),
      //数据
      T_data: [],
      //属性
      defaultPropsLeft: {
        children: 'children',
        label: 'name',
      },
      //
      cnkarr: '', // 原值：[]
      groupId: undefined,
      menuId: undefined,
    }
  },
  mounted() {
    //获得多选
    this.T_data = JSON.parse(JSON.stringify(this.CheckedBtnArr))
    let ids = []
    this.C_arr_data = []
    this.T_data.forEach((ele) => {
      this.C_arr_data.push(ele['id'])
    })
    console.log('C_arr_data', this.C_arr_data, 'T_data', this.T_data)
    // console.log(this.rowClone, this.pc, this.TreeSaveNode);
    // console.log("FFFFFFFFFFFFFFFFFFFF");
    // this.$refs.tree.setCheckedKeys(this.C_arr_data);
    this.groupId = this.rowClone['id']
    this.menuId = this.TreeSaveNode['id']
    this.getEchoFun()
  },
  watch: {
    C_arr_data: {
      handler(newValue, oldValue) {
        let ids = []
        newValue = []
        this.T_data.forEach((ele) => {
          newValue.push(ele['id'])
        })
        // this.$refs.tree.setCheckedKeys(newValue);
      },
      deep: true,
    },
    CheckedBtnArr: {
      handler(newValue, oldValue) {
        this.T_data = JSON.parse(JSON.stringify(this.CheckedBtnArr))
        // console.log("FFFFFFFFFFFFFFFFFFFF");
        // console.log(this.rowClone, this.pc, this.TreeSaveNode);
        // console.log("FFFFFFFFFFFFFFFFFFFF");

        this.CheckedBtnArrId =
          this.CheckedBtnArr.length && this.CheckedBtnArr.map((item) => item.id) // 新加保存id
        console.log('点击就来：', this.CheckedBtnArr, this.CheckedBtnArrId)
        this.groupId = this.rowClone['id']
        this.menuId = this.TreeSaveNode['id']
        this.getEchoFun()
      },
      deep: true,
    },
  },
  methods: {
    //点击节点
    NodeClick(val) {
      console.log(val)
      console.log('节点', this.cnkarr)
    },
    //节点发生变化
    checkChange(val1, val2) {
      let checkkeyarr = this.$refs.tree.getCheckedKeys()
      console.log('获得的节点::', checkkeyarr)
      this.$emit('backBtnList', checkkeyarr)
    },
    //获取回显
    getEchoFun() {
      // this.$refs.tree.setCheckedKeys([])
      elementEcho(this.groupId, this.menuId).then((Response) => {
        console.log('回显', Response)
        let arrobj = Response.data
        let keyArr = []
        arrobj.length &&
          arrobj.forEach((ele) => {
            keyArr.push(ele['id'])
          })
        this.$refs.tree.setCheckedKeys(
          (keyArr.length && keyArr) || this.CheckedBtnArrId
        )
        this.$emit('backBtnList', keyArr)
      })
    },
  },
}
</script>
